<template>
  <el-dialog
      title="模板数据"
      v-model="showFlag"
      width="70%"
      :before-close="handleTemplateClose"
  >
    <el-row :gutter="20">
      <el-col :span="12">
        <el-select
            v-model="fileType"
            placeholder="生成类型"
            @change="changeVmTemplate"
            filterable>
          <el-option
              v-for="item in generatorOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-select>
      </el-col>
      <el-col :span="12">
        <el-button @click="saveTemplate" type="primary">保存配置</el-button>
        <el-button @click="clearLocalStorage" type="danger">重置配置</el-button>
      </el-col>
    </el-row>
    <el-row>
      <el-col>
        <el-input
            type="textarea"
            class="textarea"
            :row="8"
            v-model="vmTemplate"
        />
      </el-col>
    </el-row>
  </el-dialog>
</template>
<script>
import {getTemplateByType} from "../../api/ejsGenerator.js";
import ejs from 'ejs'
import {strIsBlank} from "../../utils/util.js";
import {formatJavaCode} from "../../api/formatter.js";

export default {
  name: 'VelocityTemplate',
  props: {
    templateVisible: Boolean,
    generatorOptions: Array
  },
  watch: {
    templateVisible(newVal) {
      this.showFlag = newVal
    }
  },
  created() {
    this.vmTemplate = getTemplateByType(this.fileType)
  },
  data() {
    return {
      showFlag: this.templateVisible,
      vmTemplate: '',
      fileType: 'Entity'
    }
  },
  methods: {
    handleTemplateClose() {
      this.$emit('handle-dialog-close', 'template')
    },
    changeVmTemplate() {
      const template = getTemplateByType(this.fileType);
      console.log(template)
      this.vmTemplate = template
    },
    saveTemplate() {
      localStorage.setItem(this.fileType, this.vmTemplate)
      this.$message.success('保存成功')
    },
    clearLocalStorage() {
      for(let item of this.generatorOptions) {
        localStorage.removeItem(item.value)
      }
      this.vmTemplate = getTemplateByType(this.fileType)
      this.$message.success('重置成功')
    }
  }
}
</script>
<style></style>
